// 直接任务传递信息
// 本实验原本是用滑动变阻器控制3个led环，
// 考虑更换为滑动变阻器控制3个led的闪烁速度


#include "main.h"



static TaskHandle_t neo_ring_h = NULL;

void config_task(void *p){
  pinMode(R_PIN, INPUT);
  pinMode(G_PIN, INPUT);
  pinMode(B_PIN, INPUT);
  pinMode(S_PIN, INPUT);
  
  uint8_t r_value = 0;
  uint8_t g_value = 0;
  uint8_t b_value = 0;
  uint8_t s_value = 100;
  
  while (1){
    r_value = map(analogRead(R_PIN), 0, 4095, 0, 255);
    g_value = map(analogRead(G_PIN), 0, 4095, 0, 255);
    b_value = map(analogRead(B_PIN), 0, 4095, 0, 255);
    s_value = map(analogRead(S_PIN), 0, 4095, 10, 200);
    
    // 4个信息移位，打包成1个
    uint32_t rgb = s_value << 24 | r_value << 16 | g_value << 8 | b_value << 0;
    
    xTaskNotify(neo_ring_h, rgb, eSetValueWithoutOverwrite);
    
    vTaskDelay(100);
    

  }
  
}


void neo_ring(void *p){
  
  Adafruit_NeoPixel pixels(NUM_PIXELS, NEO_PIN, NEO_GRB + NEO_KHZ800);
  
  pixels.begin();
  
  uint32_t srgb = 0;
  uint8_t r = 0;
  uint8_t g = 0;
  uint8_t b = 0;
  uint8_t s = 100;
  
  while(1){
    pixels.clear();
    
    for(int i=0; i<NUM_PIXELS; i++){
      
      xTaskNotifyWait(0x00, 0x00, &srgb, 0);
      
      s = (srgb & 0xff000000) >> 24;
      r = (srgb & 0x00ff0000) >> 16;
      g = (srgb & 0x0000ff00) >> 8;
      b = (srgb & 0x000000ff) >> 0;
      
      pixels.setPixelColor(i, pixels.Color(r, g, b));
      pixels.show();
      
      // 根据速度delay
      vTaskDelay(s * 5);
      
    }
  }
    
}






void setup() {
  Serial.begin(115200);
  
  xTaskCreate(config_task, "", 1024*10, NULL, 1, NULL);
  xTaskCreate(neo_ring, "", 1024*20, NULL, 1, &neo_ring_h);
}

void loop() {
  // put your main code here, to run repeatedly:
}

